package Schiffeversenken;

import javax.microedition.lcdui.*;
import java.io.IOException;
import java.util.Random;

/**
 * Represents a field on the battleground. Cares the waterimage handling.
 */
public class Field
{
    // members
    private Position    _position;
    private Image[]     _waterImageList;
    private Image       _waterImage;


    // constructors
    
    /**
     * Creates a field by given position.
     * @param   <Position>  the position of the field on battleground
     */
    public Field(Position pos)
    {
        _position = pos;
        Init();
    }
    /**
     * Creates a field by given position paramters.
     * @param   <int>   X-Coordinate
     * @param   <int>   Y-Coordinate
     */
    public Field(int x, int y)
    {
        _position = new Position(x, y);
        Init();
    }


    // public methods


    // private methods

    /**
     * Initialize this Field and defines a random water image.
     */
    private void Init()
    {
        try
        {
            _waterImageList = new Image[ 10 ];
            for(int n=0; n<=9; n++)
            {
                _waterImageList[n] = Image.createImage("/water_"+n+".png");
            }
        } catch (Exception ex) {}

        reinitFieldImage();
    }

    /**
     * Redefine the field waterimage randomly.
     */
    private void reinitFieldImage()
    {
        Random myRand = new Random(GameInstance.getRandomSeed());
        _waterImage = _waterImageList[ myRand.nextInt(9) ];
    }

    
    // properties

    /**
     * @return  <Image> returns the current Waterimage of this field, resized by games static defined <b>FIELDWIDTH</b>.
     */
    public Image getWaterImage()
    {
        return GameInstance.resizeTransparentImage(_waterImage, GameInstance.FIELDWIDTH, GameInstance.FIELDWIDTH);
    }
    /**
     * @return  <Image[]>   returns an Array of all waterimage instances given by resources
     */
    public Image[] getWaterImages()
    {
        return _waterImageList;
    }
    /**
     * Sets the list of waterimage-instances.
     * @param <Image[]> list of waterimage-instances
     */
    public void setWaterImages( Image[] waterImageList )
    {
        _waterImageList = waterImageList;
    }
    /**
     * @return  <Position>  return fields position.
     */
    public Position getPosition()
    {
        return _position;
    }
    /**
     * @return  <int>   X-Coordinate of this field
     */
    public int X()
    {
        return _position.X();
    }
    /**
     * @return  <int>   Y-Coordinate of this field
     */
    public int Y()
    {
        return _position.Y();
    }


    // static methods

    /**
     * @return  <Image> returns an instance of an image displaying at a watershoot
     */
    public static Image getShootFailImage()
    {
        Image img = null;
        try
        {
            img = Image.createImage("/fail.png");
        } catch (Exception ex) {}
        return GameInstance.resizeTransparentImage(img, GameInstance.FIELDWIDTH, GameInstance.FIELDWIDTH);
    }
    /**
     * @return  <Image> returns an instance of an image displaying at a shipshoot
     */
    public static Image getShootSuccessImage()
    {
        Image img = null;
        try
        {
            img = Image.createImage("/ex.png");
        } catch (Exception ex) {}
        return GameInstance.resizeTransparentImage(img, GameInstance.FIELDWIDTH, GameInstance.FIELDWIDTH);
    }
}
